package com.loe.mvp.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.widget.Toast;

import com.loe.mvp.BuildConfig;

import java.util.UUID;

import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

public class SystemUtil
{
    public static String getDeviceId(Context context)
    {
        String deviceId = "";
        String aid = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        String serial = android.os.Build.SERIAL;
        if (serial == null || serial.startsWith("un"))
        {
            serial = "";
        }
        if (aid == null || aid.isEmpty() || aid == "null" || aid == "")
        {
            deviceId = UUID.randomUUID().toString();
        }
        else
        {
            deviceId = aid + serial;
        }
        return deviceId;
    }

    public static String getUserAgent(String versionName)
    {
        return "Android " + android.os.Build.VERSION.RELEASE + "; " + android.os.Build.BRAND + " " + android.os.Build.MODEL + "; v" + versionName;
    }

    public static boolean isDebug(Context context)
    {
        try
        {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e)
        {
            return false;
        }
    }

    public static boolean isRelease(Context context)
    {
        return !isDebug(context);
    }

    public static void callPhone(Context context, String phone)
    {
        context.startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone)));
    }

    public static int getPx(double dp)
    {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density + 0.5);
    }

    public static int getPx(int dp)
    {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density + 0.5);
    }

    public static boolean isNotificationEnabled(Context context)
    {
        try
        {
            return NotificationManagerCompat.from(context).areNotificationsEnabled();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return true;
    }

    public static void toSetNotification(Context context)
    {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26)
        {
            // android 8.0引导
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
            intent.putExtra("app_package", context.getPackageName());
            intent.putExtra("app_uid", context.getApplicationInfo().uid);
        }
        else if (Build.VERSION.SDK_INT >= 21)
        {
            // android 5.0-7.0
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", context.getPackageName());
            intent.putExtra("app_uid", context.getApplicationInfo().uid);
        }
        else
        {
            // 其他
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 复制到剪切板
     */
    public static void copyToClipboard(Context context, String text)
    {
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard != null)
        {
            ClipData clip = ClipData.newPlainText("text", text);
            clipboard.setPrimaryClip(clip);
            SystemToast.toast(context, "已复制到剪贴板");
        }
    }

    /**
     * 去应用市场下载
     */
    public static void gotoMarketDown(Context context, String packageName)
    {
        try
        {
            Uri uri = Uri.parse("market://details?id=" + packageName);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        } catch (Exception e)
        {
            SystemToast.toast(context, "未安装应用市场");
            // 打开https://sj.qq.com/appdetail/${packageName}
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://sj.qq.com/appdetail/" + packageName)));
        }
    }

    public static boolean isHuawei()
    {
        String cc = (Build.MANUFACTURER + "").toLowerCase();
        return cc.contains("huawei") || cc.contains("honor") || cc.contains("oce");
    }

    public static boolean isXiaomi()
    {
        String cc = (Build.MANUFACTURER + "").toLowerCase();
        return cc.contains("xiaomi");
    }

    public static boolean isOppo()
    {
        String cc = (Build.MANUFACTURER + "").toLowerCase();
        return cc.contains("oppo");
    }

    public static boolean isVivo()
    {
        String cc = (Build.MANUFACTURER + "").toLowerCase();
        return cc.contains("vivo");
    }

    /**
     * 检测微信是否安装
     * manifest 加上：
     * <queries>
     *     <package android:name="com.tencent.mm" />
     * </queries>
     */
    public static boolean isWeChatInstalled(Context context)
    {
        if (context == null) return false;
        try
        {
            context.getPackageManager().getPackageInfo("com.tencent.mm", 0);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }
}
